package com.qiang.test.service;



import com.qiang.test.domain.Manager;
import com.qiang.test.domain.Subway;

import java.io.*;
import java.util.ArrayList;

public class Service {

    public ArrayList<Manager> managerLoad(){
        ArrayList<Manager> list = new ArrayList<>();
        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader("kaoshi\\manager.txt"));
            String manager;
            while( (manager = br.readLine()) != null){
                String[] managers = manager.split(",");
                Manager m = new Manager(managers[0],managers[1],managers[2],managers[3]);
                list.add(m);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(br != null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return list;
    }

    public ArrayList<Subway> subwayInput(){
        ArrayList<Subway> list = new ArrayList<>();
        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader("kaoshi\\subway.txt"));
            String subway;
            while((subway = br.readLine())!= null){
                String[] subways = subway.split(",");
                list.add(new Subway(subways[0],subways[1],subways[2]));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(br != null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return list;
    }

    public void subwayOutput(ArrayList<Subway> list){
        BufferedWriter bw = null;
        try {
            bw = new BufferedWriter(new FileWriter("kaoshi\\subway.txt"));
            for (Subway subway : list) {
                String s = subway.getId()+","+ subway.getName()+","+subway.getWay();
                bw.write(s);
                bw.newLine();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(bw != null){
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
